<?php  
class GoodsGalleryModel extends Model{
	public $table='goods_gallery';
	/**
	 * 构造函数
	 * @return [type] [description]
	 */
	public function __init(){}
	/**
	 * 添加商品图片（详情页图片）添加商品时
	 */
	public function addImage($goods_gid){
		$upload = new Upload('Upload/goods/' . date('Y/m/d'));
        $img = $upload->upload('gallery_img');
        /**
         * 没有图片上传时 不进行处理
         */
        if (empty($img))return;
        $image = new Image();
        foreach ($img as $file) {
            //中图 350x350
            $img_url = $file['dir'] . $file['filename'] . '_list_350x350.' . $file['ext'];
            //小图 50x50
            $thumb_url = $file['dir'] . $file['filename'] . '_index_50x50.' . $file['ext'];

            $image->thumb($file['path'], $img_url, 350, 350, 6);
            $image->thumb($file['path'], $thumb_url, 50, 50, 6);
            $data = array(
                'goods_gid' => $goods_gid,//商品ID
                'img_original' => $file['path'],//原图
                'img_url' => $img_url,//中图
                'thumb_url' => $thumb_url//小图
            );
            M("goods_gallery")->add($data);
        }
	}
	/**
	 * 获取该商品所有详情图片 编辑时 
	 * @param  [int] $goods_gid [商品id]
	 * @return [array]            [相关图片数据]
	 */
	public function getImage($goods_gid){
		// 查询条件
		$map['goods_gid']=$goods_gid;
		return $this->where($map)->all();
	}
	/**
	 * 删除详情页图片
	 * @param  [int] $goods_gallery_id [商品图片主键id]
	 * @return [type]                   [description]
	 */
	public function delImage($goods_gallery_id){
		//查询要删除的图片
		$image=$this->find($goods_gallery_id);
		is_file($image['img_original']) and unlink($image['img_original']);//删除原图文件
		is_file($image['img_url']) and unlink($image['img_url']);//删除中图文件
		is_file($image['thumb_url']) and unlink($image['thumb_url']);//删除小图文件
		return $this->del($goods_gallery_id);//删除该条数据
	}
}
?>